package com.budwk.app.sqfwV2.generator.dayingRoadExcel;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfwDoc.bo.SqfwExcelForSummaryBO;
import com.budwk.app.sqfwDoc.bo.SqfwExcelJudgeRes;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.BaseTemplateExcelGenerator;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.SqfwExcelCreateCommonBO;
import com.budwk.app.sqfwV2.component.DayingRoadPdfComponent;
import com.budwk.app.sqfwV2.component.ProblemComponent;
import com.budwk.app.sqfwV2.dto.dto.FireSafetyOnDayingRoadPdfDTO;
import com.budwk.app.sqfwV2.dto.dto.centuryCity.SafeCheckTaskParamDataDTO;
import com.budwk.app.sqfwV2.dto.image.FireSafetyOnDayingRoadExcelImageConfigBO;
import com.budwk.app.sqfwV2.enums.DayingRoadFileServiceEnum;
import com.budwk.app.sqfwV2.enums.dayingRoad.DayingRoadProblemEnum;
import com.budwk.app.util.StrUtil;
import com.budwk.app.util.poiUtils.CommonFormOfficeUtils;
import com.budwk.app.util.poiUtils.excelPictureConfigBO.ExcelFileCreateParamBO;
import com.deepoove.poi.data.PictureRenderData;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 12 社会治安重点风险场所台账/Excel
 */

@Slf4j
public class SecurityKeyBookGenerator extends BaseTemplateExcelGenerator implements DayingRoadExcelGenerator {

    @Override
    protected String getTemplateName() {
        return DayingRoadFileServiceEnum.DAYING_ROAD_EXCEL_SITES.getFilePath();
    }

    /**
    * 可以参照 {@link com.budwk.app.sqfw.enums.DownLoadFileNameEnum}
    *
    * @return 文件名名称
    */
    @Override
    public String getDownloadFileName() {
        //TODO 在这里返回你生成的文件名名称，注意注意注意不用带上后缀
        return DayingRoadPdfComponent.getDictName(DayingRoadFileServiceEnum.DAYING_ROAD_EXCEL_SITES.getCode(), null);
    }

    /**
     * 用户自定义excel基础参数，比如表头这样的公用数据
     * @param excelParam 已经填充的基础数据据
     * @param excelCreateParam excel生成提供的原料
     */
    @Override
    protected void customerPopularSheetBaseParam(Map<String, Object> excelParam, SqfwExcelCreateCommonBO excelCreateParam) {
        log.info("SecurityKeyBookGenerator BaseExcelParam: {}", JSONObject.toJSONString(excelCreateParam.getBaseExcelParamDTO()));
        excelParam.put("county", excelCreateParam.getBaseExcelParamDTO().getCounty());
    }

    @Override
    protected void customerPopularSheetDataParam(List<Map<String, Object>> listMap, SqfwExcelCreateCommonBO excelCreateParam) {
        List<Sqfw_formSummary_new> summaryList = DayingRoadPdfComponent.getSummaryList(listMap, excelCreateParam);
        log.info("SecurityKeyBookGenerator customerPopularSheetDataParam summaryList: {}", JSONObject.toJSONString(summaryList));
        int num = 1;
        for (Sqfw_formSummary_new summary : summaryList) {
            FireSafetyOnDayingRoadPdfDTO roadPdfDTO = JSONObject.parseObject(summary.getParamData(), FireSafetyOnDayingRoadPdfDTO.class);
            log.info("SecurityKeyBookGenerator customerPopularSheetDataParam roadPdfDTO: {}", JSONObject.toJSONString(roadPdfDTO));
            if (!isGenerateExcel(roadPdfDTO)) {
                continue;
            }
            // 构建数据
            Map<String, Object> map = buildExcelMap(roadPdfDTO);
            map.put("num", num ++);
            map.put("communityStr", summary.getCommunity());//社区
            listMap.add(map);
        }
        log.info("SecurityKeyBookGenerator customerPopularSheetDataParam end: {}", JSONObject.toJSONString(listMap));
    }

    private Map<String, Object> buildExcelMap(FireSafetyOnDayingRoadPdfDTO roadPdfDTO) {
        Map<String, Object> map = new HashMap<>();
        map.put("placeName", StrUtil.replaceNullStr(roadPdfDTO.getPlaceName()));
        map.put("address", StrUtil.replaceNullStr(roadPdfDTO.getAddress()));
        map.put("operator", StrUtil.replaceNullStr(roadPdfDTO.getOperator()));
        map.put("operatorTelephone", StrUtil.replaceNullStr(roadPdfDTO.getOperatorTelephone()));
        map.put("mainBusinessScopeName", DayingRoadPdfComponent.getDictName(roadPdfDTO.getMainBusinessScope(), null));
        // 具体分类-》行业领域
        List<String> names = DayingRoadPdfComponent.getIndustrySectorStr(roadPdfDTO.getIndustrySector());
        map.put("industrySectorName", String.join(",", names));

        // 其他隐患
        List<SafeCheckTaskParamDataDTO> others = roadPdfDTO.getOtherHiddenDangers();
        map.put("isOtherHiddenDangers", "");
        map.put("otherHiddenDangers", "");
        if (others != null && others.size() > 0) {
            SafeCheckTaskParamDataDTO s = others.get(others.size() - 1);
            if (DayingRoadProblemEnum.TWELVE.getDefaultValue().equals(s.getKey())) {
                map.put("isOtherHiddenDangers", s.getRectificationMeasure());
                map.put("otherHiddenDangers", s.getRectificationMeasure());
            }else {
                map.put("isOtherHiddenDangers", "有");
                map.put("otherHiddenDangers", s.getRectificationMeasure());
            }
        }
        //走访图片
        String pictureUrlStr = roadPdfDTO.getVisitPictures();
        List<Map<String, PictureRenderData>> visitPictures = Lists.newArrayList();
        map.put("visitPictures", visitPictures);
        if (StringUtils.isNotBlank(pictureUrlStr)) {
            map.put("visitPictures", CommonFormOfficeUtils.addPictureToByteList(roadPdfDTO.getVisitPictures(), 5));
        }
        //巡查时间
        map.put("inspectionDate", DateUtil.format(roadPdfDTO.getInspectionDate(), DatePattern.NORM_DATETIME_PATTERN));
        map.put("checkedUnitLeader", "");
        if (StringUtils.isNotEmpty(roadPdfDTO.getCheckedUnitLeader())) {
            map.put("checkedUnitLeader", CommonFormOfficeUtils.addPictureToByteList(roadPdfDTO.getCheckedUnitLeader(), 1));
        }
        return map;
    }

    public boolean isGenerateExcel(FireSafetyOnDayingRoadPdfDTO roadPdfDTO) {
        return false;
    }

    @Override
    protected void completeExcelFileCreateParam(ExcelFileCreateParamBO excelFileCreateParam) {

    }

    @Override
    protected void customerPopularRowDataMap(Map<String, Object> rowDataMap, SqfwExcelForSummaryBO forSummaryBO) {

    }

    /**
     * 如果在遍历之前就能够判断excel是否生成，那么就在这个方法中进行判断
     * @param excelCreateParam excel生成所需的最为全面的数据
     * @param excelParam excel已经填充的部分数据
     */
    @Override
    protected SqfwExcelJudgeRes judgeDataIntoExcelBeforeErgodic(SqfwExcelCreateCommonBO excelCreateParam, Map<String, Object> excelParam) {
        return DayingRoadPdfComponent.getSqfwExcelJudgeRes(excelCreateParam, new SecurityKeyBookGenerator());
    }

    /**
    * 如果在遍历之后能够判断excel是否生成，那么就在这个方法中进行判断
    * @param excelParam excel已经填充的部分数据
    */
    @Override
    protected SqfwExcelJudgeRes judgeDataIntoExcelBeforeCreate(Map<String, Object> excelParam) {
        return null;
    }

    @Override
    protected Class<?> getImageConfigBO() {
        return FireSafetyOnDayingRoadExcelImageConfigBO.class;
    }
}
